// 接口根域名
axios.defaults.baseURL = 'http://123.57.109.30:3007';

// 忽略提示的url
let ignoreTipsUrlList = [
    '/my/article/cates',
    '/my/userinfo',
    '/my/updatepwd',
    '/my/article/list'
];

// 请求拦截器
axios.interceptors.request.use(function (config) {
    if (config.url.startsWith('/my')) {
        config.headers['Authorization'] = sessionStorage.getItem('token');
    }
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(function (response) {
    if (ignoreTipsUrlList.indexOf(response.config.url) == -1) {
        var layer = layui.layer;
        layer.msg(response.data.message, {id: 'hwh'});
    }
    if (response.data.status == 1) {
        return Promise.reject(response.data.message);
    }
    // 对响应数据做点什么
    return response;
}, function (error) {
    if (error.response.status == 403) {
        layer.msg(error.response.data.message, {
            icon: 2,
            end: function () {
                window.parent.location.href = '/login.html';
            }
        })
        return Promise.reject(error.response.data.message);
    }
    // 对响应错误做点什么
    return Promise.reject(error);
});

// 注册api
const registerAPI = (data, fn) => axios({
    url: '/api/reguser',
    method: 'POST',
    data: data
}).then(fn)

// 登陆api
const loginAPI = (data, fn) => axios({
    url: '/api/login',
    method: 'POST',
    data
}).then(fn);

// 添加类别
const addCatesAPI = (data, fn) => axios({
    url: '/my/article/addcates',
    method: 'POST',
    data
}).then(fn);

// 类别列表
const catesListAPI = (data, fn) => axios({
    url: '/my/article/cates',
    method: 'GET',
    data
}).then(fn);

// 类别删除
const delCatesAPI = (data, fn) => axios({
    url: '/my/article/deletecate/' + data.id,
    method: 'GET',
}).then(fn);

// 类别修改
const editCatesAPI = (data, fn) => axios({
    url: '/my/article/updatecate',
    method: 'POST',
    data
}).then(fn);

// 获取用户的基本信息
const getUserInfoAPI = (data, fn) => axios({
    url: '/my/userinfo',
    method: 'GET',
}).then(fn);

// 更新用户的基本信息
const upUserInfoAPI = (data, fn) => axios({
    url: '/my/userinfo',
    method: 'POST',
    data
}).then(fn);

// 重置密码
const updatePwdAPI = (data, fn) => axios({
    url: '/my/updatepwd',
    method: 'POST',
    data
}).then(fn);

// 更换头像
const avatarAPI = (data, fn) => axios({
    url: '/my/update/avatar',
    method: 'POST',
    data
}).then(fn);

// 发布新文章
const addArticleAPI = (data, fn) => axios({
    url: '/my/article/add',
    method: 'POST',
    data
}).then(fn);

// 获取文章列表
const ArticleListAPI = (data, fn) => axios({
    url: '/my/article/list',
    method: 'GET',
    params: data
}).then(fn);

// 根据 Id 获取文章详情
const ArticleIdAPI = (data, fn) => axios({
    url: '/my/article/' + data,
    method: 'GET',
}).then(fn);

// 更新文章
const editArticleAPI = (data, fn) => axios({
    url: '/my/article/edit',
    method: 'POST',
    data
}).then(fn);

// 删除文章
const delArticleAPI = (data, fn) => axios({
    url: '/my/article/delete/' + data,
    method: 'GET',
}).then(fn);